//
//  StockDetailViewController.swift
//  股票框架
//
//  Created by 李小骥 on 2017*10*12.
//  Copyright © 2017年 李小骥. All rights reserved.
//

import UIKit
import SnapKit
class StockDetailViewController: UIViewController {
    
    //    @IBOutlet weak var headBottomView: UIView!
    //    @IBOutlet weak var headTopView: StockDetailHeadTopView!
    var xianjia = UILabel()
    var zhangdiee = UILabel()
    var zhangdiefu = UILabel()
    var jinkai = UILabel()
    var jinkaiValue = UILabel()
    var chengjiaoliang = UILabel()
    var chengjiaoliangValue = UILabel()
    var zuoshou = UILabel()
    var zuoshouValue = UILabel()
    var huanshoulv = UILabel()
    var huanshoulvValue = UILabel()
    
    var zuigao = UILabel()
    var zuigaoValue = UILabel()
    var zuidi = UILabel()
    var zuidiValue = UILabel()
    var chengbene = UILabel()
    var chengbeneValue = UILabel()
    var zhangting = UILabel()
    var zhangtingValue = UILabel()
    var dieting = UILabel()
    var dietingValue = UILabel()
    var zongguben = UILabel()
    var zonggubenValue = UILabel()
    var zhenfu = UILabel()
    var zhenfuValue = UILabel()
    var liutongzhi = UILabel()
    var liutongzhiValue = UILabel()
    
    var segmentMenu: SegmentMenu!
    var stockBriefView: HSStockBriefView?
    var kLineBriefView: HSKLineBriefView?
    var currentShowingChartVC: UIViewController?
    var controllerArray : [UIViewController] = []
    var timer:Timer!
    var stockCode:String!
    func setTimer(){
        weak var weakSelf:StockDetailViewController? = self
        timer = Timer.scheduledTimer(withTimeInterval: requestTimeInterval, repeats: true, block: { (_) in
            weakSelf?.loadStockDetailData()
        })
    }
    func loadStockDetailData(){
        weak var weakSelf:StockDetailViewController? = self
        Utils.AlamofireAsynchronousGETStockDetailData(code: stockCode) { (result) in
            let title = result!.name + "(" + result!.code + ")"
            weakSelf?.navigationItem.title = title
            weakSelf?.navigationController?.navigationBar.titleTextAttributes = (naviBarTitleAttributes as! [NSAttributedStringKey : Any])
            let color = (result!.change.hasPrefix("-")) ? UIColor.Drop : UIColor.APP
            weakSelf?.navigationController?.navigationBar.barTintColor = color
            weakSelf?.xianjia.text = result!.price
            weakSelf?.xianjia.textColor = color
            weakSelf?.zhangdiee.text = result!.change
            weakSelf?.zhangdiee.textColor = color
            weakSelf?.zhangdiefu.text = result!.changePct
            weakSelf?.zhangdiefu.textColor = color
            weakSelf?.jinkaiValue.text = result!.openPrice
            weakSelf?.chengjiaoliangValue.text = String(format: "%.2f",(Double(result!.volumn)! / 1000000.0)) + "万手"
            weakSelf?.zuoshouValue.text = result!.closePrice
            weakSelf?.huanshoulvValue.text = result!.turnoverRate
            weakSelf?.zuigaoValue.text = String(format: "%.2f",(Double(result!.highPrice)!))
            weakSelf?.zuidiValue.text = String(format: "%.2f",(Double(result!.lowPrice)!))
            weakSelf?.chengbeneValue.text = String(format: "%.2f",(Double(result!.volumnPrice)! / 100000000.0)) + "亿"
            weakSelf?.zhangtingValue.text = String(format: "%.2f",(Double(result!.closePrice)! * 1.1))
            weakSelf?.dietingValue.text = String(format: "%.2f",(Double(result!.closePrice)! * 0.9))
            weakSelf?.zonggubenValue.text = String(format: "%.2f",(Double(result!.totalShares)! / 100000000.0)) + "亿"
            weakSelf?.zhenfuValue.text = result!.swing
            weakSelf?.liutongzhiValue.text = String(format: "%.2f",(Double(result!.circulationValue)! / 100000000.0)) + "亿"
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .Background
        addViews()
        setViews()
        loadStockDetailData()
        setWidth()
        setUpView()
        addChartController()
        segmentMenu.setSelectButton(index: 1)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        if(timer != nil){
        timer.invalidate()
        }
    }
    func setUpView() {
        //        分时，日 K，周 K，月 K 的 view 条，是一个包含 button 数组的 View，里面有根据不同情况设置 button 的方法，它的 Y 高度紧接着头部视图
        segmentMenu = SegmentMenu(frame: CGRect(x: 0, y: 64+100, width: ScreenWidth, height: 40))
        //        分时 日K。。。的数组源
        segmentMenu.menuTitleArray = ["分时", /*"五日",*/ "日K", "周K", "月K"]
        
        segmentMenu.delegate = self
        //        时间，价，幅，量的标签条，长按分时图时候显示
        //        stockBriefView = HSStockBriefView(frame: CGRect(x: 0, y: headerStockInfoView.frame.maxY, width: self.view.frame.width, height: 40))
        //        stockBriefView?.isHidden = true
        // K 线图的详情信息的标签条，长按日 K，月 K，周 K 的 K 线图时候显示
        //        kLineBriefView = HSKLineBriefView(frame: CGRect(x: 0, y: headerStockInfoView.frame.maxY, width: self.view.frame.width, height: 40))
        //        kLineBriefView?.isHidden = true
        
        self.view.addSubview(segmentMenu)
        //        self.view.addSubview(stockBriefView!)
        //        self.view.addSubview(kLineBriefView!)
    }
    func addChartController() {
        // 分时线
        let timeViewcontroller = ChartViewController()
        timeViewcontroller.chartType = HSChartType.timeLineForDay
        controllerArray.append(timeViewcontroller)
        
        // 五日分时线
        //        let fiveDayTimeViewController = ChartViewController()
        //        fiveDayTimeViewController.chartType = HSChartType.timeLineForFiveday
        //        controllerArray.append(fiveDayTimeViewController)
        //
        // 日 K 线
        let kLineViewController = ChartViewController()
        kLineViewController.chartType = HSChartType.kLineForDay
        controllerArray.append(kLineViewController)
        
        // 周 K 线
        let weeklyKLineViewController = ChartViewController()
        weeklyKLineViewController.chartType = HSChartType.kLineForWeek
        controllerArray.append(weeklyKLineViewController)
        
        // 月 K 线
        let monthlyKLineViewController = ChartViewController()
        monthlyKLineViewController.chartType = HSChartType.kLineForMonth
        controllerArray.append(monthlyKLineViewController)
    }
    
    func addViews(){
        //        参考
        self.xianjia = UILabel(x: 48, y: 64+16, height: 18, text: "3.76", font:18 , aliment: .center, color: .APP)
        xianjia.adjustsFontSizeToFitWidth = true
        self.view.addSubview(xianjia)
        self.zhangdiee = UILabel(x: 20, y: xianjia.y+xianjia.height+15, height: 13, text: "-0.04", font: 13, aliment: .center, color: .APP)
        zhangdiee.adjustsFontSizeToFitWidth = true
        self.view.addSubview(zhangdiee)
        self.zhangdiefu = UILabel(x: 20+zhangdiee.width+10, y: zhangdiee.y, height: 13, text: "-0.04%", font: 13, aliment: .center, color: .APP)
        zhangdiefu.adjustsFontSizeToFitWidth = true
        self.view.addSubview(zhangdiefu)
        self.jinkai = UILabel(x: 48+xianjia.width+48, y: xianjia.y, height: 13, text: "今开", font: 13, aliment: .center, color: .LabelFont)
        jinkai.adjustsFontSizeToFitWidth = true
        self.view.addSubview(jinkai)
        self.jinkaiValue = UILabel(x: jinkai.x+jinkai.width+20, y: jinkai.y, height: 13, text: "3.820", font: 13, aliment: .center, color: .LabelFont)
        jinkaiValue.adjustsFontSizeToFitWidth = true
        self.view.addSubview(jinkaiValue)
        self.chengjiaoliang = UILabel(x: zhangdiefu.x+zhangdiefu.width+20, y: zhangdiee.y, height: 13, text: "成交量", font: 13, aliment: .center, color: .LabelFont)
        chengjiaoliang.adjustsFontSizeToFitWidth = true
        self.view.addSubview(chengjiaoliang)
        self.chengjiaoliangValue = UILabel(x: chengjiaoliang.x+chengjiaoliang.width+10, y: zhangdiee.y, height: 13, text: "1250万手", font: 13, aliment: .center, color: .LabelFont)
        chengjiaoliangValue.adjustsFontSizeToFitWidth = true
        self.view.addSubview(chengjiaoliangValue)
        self.huanshoulv = UILabel(x: chengjiaoliangValue.x+chengjiaoliangValue.width+20, y: zhangdiee.y, height: 13, text: "换手率", font: 13, aliment: .center, color: .LabelFont)
        huanshoulv.adjustsFontSizeToFitWidth = true
        self.view.addSubview(huanshoulv)
        self.huanshoulvValue = UILabel(x: huanshoulv.x+huanshoulv.width+10, y: zhangdiee.y, height: 13, text: "1.250%", font: 13, aliment: .center, color: .LabelFont)
        huanshoulvValue.adjustsFontSizeToFitWidth = true
        self.view.addSubview(huanshoulvValue)
        self.zuoshou = UILabel(x: huanshoulv.x, y: xianjia.y, height: 13, text: "昨收", font: 13, aliment: .center, color: .LabelFont)
        zuoshou.adjustsFontSizeToFitWidth = true
        self.view.addSubview(zuoshou)
        self.zuoshouValue = UILabel(x: huanshoulvValue.x, y: xianjia.y, height: 13, text: "3.820", font: 13, aliment: .center, color: .LabelFont)
        zuoshouValue.adjustsFontSizeToFitWidth = true
        self.view.addSubview(zuoshouValue)
        
        self.zuigao = UILabel(x: 15, y: 64+70+16, height: 15, text: "最高", font: 15, aliment: .center, color: .LabelFont)
        zuigao.adjustsFontSizeToFitWidth = true
        self.view.addSubview(zuigao)
        self.zuigaoValue = UILabel(x: zuigao.x+zuigao.width+15, y: zuigao.y, height: 15, text: "3.850", font: 15, aliment: .center, color: .LabelFont)
        zuigaoValue.adjustsFontSizeToFitWidth = true
        self.view.addSubview(zuigaoValue)
        self.zhangting = UILabel(x: zuigao.x, y: zuigao.y+zuigao.height+10, height: 15, text: "涨停", font: 15, aliment: .center, color: .LabelFont)
        zhangting.adjustsFontSizeToFitWidth = true
        self.view.addSubview(zhangting)
        self.zhangtingValue = UILabel(x: zhangting.x+zhangting.width+15, y: zhangting.y, height: 15, text: "3.850", font: 15, aliment: .center, color: .LabelFont)
        zhangtingValue.adjustsFontSizeToFitWidth = true
        self.view.addSubview(zhangtingValue)
        self.zhenfu = UILabel(x: zuigao.x, y: zhangting.y+zhangting.height+10, height: 15, text: "振幅", font: 15, aliment: .center, color: .LabelFont)
        zhenfu.adjustsFontSizeToFitWidth = true
        self.view.addSubview(zhenfu)
        self.zhenfuValue = UILabel(x: zhenfu.x+zhenfu.width+15, y: zhenfu.y, height: 15, text: "3.850", font: 15, aliment: .center, color: .LabelFont)
        zhenfuValue.adjustsFontSizeToFitWidth = true
        self.view.addSubview(zhenfuValue)
        self.liutongzhi = UILabel(x: zuigao.x, y: zhangting.y+zhangting.height+10, height: 15, text: "流通值", font: 15, aliment: .center, color: .LabelFont)
        liutongzhi.adjustsFontSizeToFitWidth = true
        self.view.addSubview(liutongzhi)
        self.liutongzhiValue = UILabel(x: liutongzhi.x+liutongzhi.width+15, y: liutongzhi.y, height: 15, text: "3.850", font: 15, aliment: .center, color: .LabelFont)
        liutongzhiValue.adjustsFontSizeToFitWidth = true
        self.view.addSubview(liutongzhiValue)
        self.zuidi = UILabel(x: liutongzhi.x, y: zuigao.y, height: 15, text: "最低", font: 15, aliment: .center, color: .LabelFont)
        zuidi.adjustsFontSizeToFitWidth = true
        self.view.addSubview(zuidi)
        self.zuidiValue = UILabel(x: liutongzhiValue.x, y: zuigao.y, height: 15, text: "3.850", font: 15, aliment: .center, color: .LabelFont)
        zuidiValue.adjustsFontSizeToFitWidth = true
        self.view.addSubview(zuidiValue)
        self.dieting = UILabel(x: liutongzhi.x, y: zhangting.y, height: 15, text: "跌停", font: 15, aliment: .center, color: .LabelFont)
        dieting.adjustsFontSizeToFitWidth = true
        self.view.addSubview(dieting)
        self.dietingValue = UILabel(x: liutongzhiValue.x, y: zhangting.y, height: 15, text: "3.850", font: 15, aliment: .center, color: .LabelFont)
        dietingValue.adjustsFontSizeToFitWidth = true
        self.view.addSubview(dietingValue)
        self.chengbene = UILabel(x: zuidiValue.x+zuidiValue.width+45, y: zuigao.y, height: 15, text: "成本额", font: 15, aliment: .center, color: .LabelFont)
        chengbene.adjustsFontSizeToFitWidth = true
        self.view.addSubview(chengbene)
        self.chengbeneValue = UILabel(x: chengbene.x+chengbene.width+15, y: zuigao.y, height: 15, text: "3.850", font: 15, aliment: .center, color: .LabelFont)
        chengbeneValue.adjustsFontSizeToFitWidth = true
        self.view.addSubview(chengbeneValue)
        self.zongguben = UILabel(x: chengbene.x, y: zhangting.y, height: 15, text: "总股本", font: 15, aliment: .center, color: .LabelFont)
        zongguben.adjustsFontSizeToFitWidth = true
        self.view.addSubview(zongguben)
        self.zonggubenValue = UILabel(x: chengbeneValue.x, y: zhangting.y, height: 15, text: "3.850", font: 15, aliment: .center, color: .LabelFont)
        zonggubenValue.adjustsFontSizeToFitWidth = true
        self.view.addSubview(zonggubenValue)
        let thinLine = UIImageView(frame: CGRect(x: 0, y: 64, width: view.bounds.width, height: 1))
        thinLine.backgroundColor = .ThinLine
        view.addSubview(thinLine)
    }
    func setViews(){
//        self.xianjia = UILabel(x: 48, y: 64+16, height: 18, text: "3.76", font:18 , aliment: .center, color: .APP)
        xianjia.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.top.equalTo(16)//64+16
            make.width.equalTo(80)
        }
        zhangdiee.snp.makeConstraints { (make) in
            make.left.equalTo(5)
            make.top.equalTo(xianjia.snp.bottom).offset(5)
            make.width.equalTo(50)
        }
        zhangdiefu.snp.makeConstraints { (make) in
            make.left.equalTo(zhangdiee.snp.right)
            make.top.equalTo(xianjia.snp.bottom).offset(5)
            make.width.equalTo(50)
        }
        jinkai.snp.makeConstraints { (make) in
            make.left.equalTo(xianjia.snp.right).offset(30)
            make.top.equalTo(xianjia.snp.top)
            make.width.equalTo(40)
        }
        jinkaiValue.snp.makeConstraints { (make) in
            make.left.equalTo(xianjia.snp.right).offset(70)
            make.top.equalTo(xianjia.snp.top)
            make.width.equalTo(40)
        }
        zuoshou.snp.makeConstraints { (make) in
            make.left.equalTo(jinkaiValue.snp.right).offset(10)
            make.top.equalTo(xianjia.snp.top)
            make.width.equalTo(40)
        }
        zuoshouValue.snp.makeConstraints { (make) in
            make.left.equalTo(jinkaiValue.snp.right).offset(60)
            make.top.equalTo(xianjia.snp.top)
            make.width.equalTo(40)
        }
        chengjiaoliang.snp.makeConstraints { (make) in
            make.left.equalTo(zhangdiefu.snp.right).offset(10)
            make.top.equalTo(zhangdiee.snp.top)
            make.width.equalTo(40)
        }
        chengjiaoliangValue.snp.makeConstraints { (make) in
            make.left.equalTo(zhangdiefu.snp.right).offset(60)
            make.top.equalTo(zhangdiee.snp.top)
            make.width.equalTo(50)
        }
        huanshoulv.snp.makeConstraints { (make) in
            make.left.equalTo(chengjiaoliangValue.snp.right).offset(5)
            make.top.equalTo(zhangdiee.snp.top)
            make.width.equalTo(40)
        }
        huanshoulvValue.snp.makeConstraints { (make) in
            make.left.equalTo(chengjiaoliangValue.snp.right).offset(50)
            make.top.equalTo(zhangdiee.snp.top)
            make.width.equalTo(50)
        }
        zuigao.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.top.equalTo(64+16)//64+70+16
            make.width.equalTo(45)
        }
        zuigaoValue.snp.makeConstraints { (make) in
            make.top.equalTo(zuigao.snp.top)
            make.left.equalTo(zuigao.snp.right).offset(5)
            make.width.equalTo(zuigao.snp.width)
        }
        zuidi.snp.makeConstraints { (make) in
            make.top.equalTo(zuigao.snp.top)
            make.left.equalTo(zuigaoValue.snp.right).offset(5)
            make.width.equalTo(zuigao.snp.width)
        }
        zuidiValue.snp.makeConstraints { (make) in
            make.top.equalTo(zuigao.snp.top)
            make.left.equalTo(zuidi.snp.right).offset(5)
            make.width.equalTo(zuigao.snp.width)
        }
        chengbene.snp.makeConstraints { (make) in
            make.top.equalTo(zuigao.snp.top)
            make.left.equalTo(zuidiValue.snp.right).offset(5)
            make.width.equalTo(zuigao.snp.width)
        }
        chengbeneValue.snp.makeConstraints { (make) in
            make.top.equalTo(zuigao.snp.top)
            make.left.equalTo(chengbene.snp.right).offset(5)
            make.width.equalTo(zuigao.snp.width)
        }
        zhangting.snp.makeConstraints { (make) in
            make.top.equalTo(zuigao.snp.bottom).offset(10)
            make.left.equalTo(zuigao.snp.left)
            make.width.equalTo(zuigao.snp.width)
        }
        zhangtingValue.snp.makeConstraints { (make) in
            make.top.equalTo(zhangting.snp.top)
            make.left.equalTo(zhangting.snp.right).offset(5)
            make.width.equalTo(zuigao.snp.width)
        }
        dieting.snp.makeConstraints { (make) in
            make.top.equalTo(zhangting.snp.top)
            make.left.equalTo(zhangtingValue.snp.right).offset(5)
            make.width.equalTo(zuigao.snp.width)
        }
        dietingValue.snp.makeConstraints { (make) in
            make.top.equalTo(zhangting.snp.top)
            make.left.equalTo(dieting.snp.right).offset(5)
            make.width.equalTo(zuigao.snp.width)
        }
        zongguben.snp.makeConstraints { (make) in
            make.top.equalTo(zhangting.snp.top)
            make.left.equalTo(dietingValue.snp.right).offset(5)
            make.width.equalTo(zuigao.snp.width)
        }
        zonggubenValue.snp.makeConstraints { (make) in
            make.top.equalTo(zhangting.snp.top)
            make.left.equalTo(zongguben.snp.right).offset(5)
            make.width.equalTo(zuigao.snp.width)
        }
        zhenfu.snp.makeConstraints { (make) in
            make.top.equalTo(zhangting.snp.bottom).offset(10)
            make.left.equalTo(zuigao.snp.left)
            make.width.equalTo(zuigao.snp.width)
        }
        zhenfuValue.snp.makeConstraints { (make) in
            make.top.equalTo(zhenfu.snp.top)
            make.left.equalTo(zhenfu.snp.right).offset(5)
            make.width.equalTo(zuigao.snp.width)
        }
        liutongzhi.snp.makeConstraints { (make) in
            make.top.equalTo(zhenfu.snp.top)
            make.left.equalTo(zhenfuValue.snp.right).offset(5)
            make.width.equalTo(zuigao.snp.width)
        }
        liutongzhiValue.snp.makeConstraints { (make) in
            make.top.equalTo(zhenfu.snp.top)
            make.left.equalTo(liutongzhi.snp.right).offset(5)
            make.width.equalTo(80)
        }
    }
    func setWidth(){
        let diyipaiquanchang = zuoshouValue.x+zuoshouValue.width+20
        let diyipaibili = view.bounds.width/diyipaiquanchang
        xianjia.frame = CGRect(x: xianjia.frame.minX*diyipaibili, y: xianjia.frame.minY, width: xianjia.frame.width*diyipaibili, height: xianjia.frame.height)
        jinkai.frame = CGRect(x: jinkai.frame.minX*diyipaibili, y: jinkai.frame.minY, width: jinkai.frame.width*diyipaibili, height: jinkai.frame.height)
        jinkaiValue.frame = CGRect(x: jinkaiValue.frame.minX*diyipaibili, y: jinkaiValue.frame.minY, width: jinkaiValue.frame.width*diyipaibili, height: jinkaiValue.frame.height)
        zuoshou.frame = CGRect(x: zuoshou.frame.minX*diyipaibili, y: zuoshou.frame.minY, width: zuoshou.frame.width*diyipaibili, height: zuoshou.frame.height)
        zuoshouValue.frame = CGRect(x: zuoshouValue.frame.minX*diyipaibili, y: zuoshouValue.frame.minY, width: zuoshouValue.frame.width*diyipaibili, height: zuoshouValue.frame.height)
        let dierpaiquanchang = huanshoulvValue.x + huanshoulvValue.width + 20
        let dierpaibili = view.bounds.width/dierpaiquanchang
        zhangdiee.frame = CGRect(x: zhangdiee.frame.minX*dierpaibili, y: zhangdiee.frame.minY, width: zhangdiee.frame.width*dierpaibili, height: zhangdiee.frame.height)
        zhangdiefu.frame = CGRect(x: zhangdiefu.frame.minX*dierpaibili, y: zhangdiefu.frame.minY, width: zhangdiefu.frame.width*dierpaibili, height: zhangdiefu.frame.height)
        chengjiaoliang.frame = CGRect(x: chengjiaoliang.frame.minX*dierpaibili, y: chengjiaoliang.frame.minY, width: chengjiaoliang.frame.width*dierpaibili, height: chengjiaoliang.frame.height)
        chengjiaoliangValue.frame = CGRect(x: chengjiaoliangValue.frame.minX*dierpaibili, y: chengjiaoliangValue.frame.minY, width: chengjiaoliangValue.frame.width*dierpaibili, height: chengjiaoliangValue.frame.height)
        huanshoulv.frame = CGRect(x: huanshoulv.frame.minX*dierpaibili, y: huanshoulv.frame.minY, width: huanshoulv.frame.width*dierpaibili, height: huanshoulv.frame.height)
        huanshoulvValue.frame = CGRect(x: huanshoulvValue.frame.minX*dierpaibili, y: huanshoulvValue.frame.minY, width: huanshoulvValue.frame.width*dierpaibili, height: huanshoulvValue.frame.height)
        let disanpaiquanchang = chengbeneValue.x + chengbeneValue.width + 15
        let disanpaibili = view.bounds.width/disanpaiquanchang
        zuigao.frame = CGRect(x: zuigao.frame.minX*disanpaibili, y: zuigao.frame.minY, width: zuigao.frame.width*disanpaibili, height: zuigao.frame.height)
        zuigaoValue.frame = CGRect(x: zuigaoValue.frame.minX*disanpaibili, y: zuigaoValue.frame.minY, width: zuigaoValue.frame.width*disanpaibili, height: zuigaoValue.frame.height)
        zuidi.frame = CGRect(x: zuidi.frame.minX*disanpaibili, y: zuidi.frame.minY, width: zuidi.frame.width*disanpaibili, height: zuidi.frame.height)
        zuidiValue.frame = CGRect(x: zuidiValue.frame.minX*disanpaibili, y: zuidiValue.frame.minY, width: zuidiValue.frame.width*disanpaibili, height: zuidiValue.frame.height)
        chengbene.frame = CGRect(x: chengbene.frame.minX*disanpaibili, y: chengbene.frame.minY, width: chengbene.frame.width*disanpaibili, height: chengbene.frame.height)
        chengbeneValue.frame = CGRect(x: chengbeneValue.frame.minX*disanpaibili, y: chengbeneValue.frame.minY, width: chengbeneValue.frame.width*disanpaibili, height: chengbeneValue.frame.height)
        let disipaiquanchang = zonggubenValue.x + zonggubenValue.width + 15
        let disipaibili = view.bounds.width/disipaiquanchang
        zhangting.frame = CGRect(x: zhangting.frame.minX*disipaibili, y: zhangting.frame.minY, width: zhangting.frame.width*disipaibili, height: zhangting.frame.height)
        zhangtingValue.frame = CGRect(x: zhangtingValue.frame.minX*disipaibili, y: zhangtingValue.frame.minY, width: zhangtingValue.frame.width*disipaibili, height: zhangtingValue.frame.height)
        dieting.frame = CGRect(x: dieting.frame.minX*disipaibili, y: dieting.frame.minY, width: dieting.frame.width*disipaibili, height: dieting.frame.height)
        dietingValue.frame = CGRect(x: dietingValue.frame.minX*disipaibili, y: dietingValue.frame.minY, width: dietingValue.frame.width*disipaibili, height: dietingValue.frame.height)
        zongguben.frame = CGRect(x: zongguben.frame.minX*disipaibili, y: zongguben.frame.minY, width: zongguben.frame.width*disipaibili, height: zongguben.frame.height)
        zonggubenValue.frame = CGRect(x: zonggubenValue.frame.minX*disipaibili, y: zonggubenValue.frame.minY, width: zonggubenValue.frame.width*disipaibili, height: zonggubenValue.frame.height)
    }
    
    //
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     //
    override func viewWillAppear(_ animated: Bool) {
        navigationController?.navigationBar.setBackgroundColor(.APP)
        makeReturnButton()
    }
    func makeReturnButton(){
        let bki = UIBarButtonItem()
        let btn = UIButton(type: .custom)
        btn.tintColor = .white
        btn.setTitle(" < ", for: .normal)
        btn.addTarget(self, action: #selector(backNative), for: .touchUpInside)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        btn.setTitleColor(.white, for: .normal)
        btn.sizeToFit()
        btn.contentHorizontalAlignment = .left
        btn.contentEdgeInsets = UIEdgeInsets(top: 0, left: -15, bottom: 0, right: 0)
        btn.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
        bki.customView = btn
        navigationItem.leftBarButtonItem = bki
    }
    @objc func backNative(){
        navigationController?.dismiss(animated: true, completion: nil)
    }
    
}

extension StockDetailViewController: SegmentMenuDelegate {
    //    不显示现有分时图或者 k 线图，显示点击 button 对应的分时图或者 k 线图
    func menuButtonDidClick(index: Int) {
        currentShowingChartVC?.willMove(toParentViewController: nil)
        currentShowingChartVC?.view.removeFromSuperview()
        currentShowingChartVC?.removeFromParentViewController()
        
        let selectedVC = self.controllerArray[index] as! ChartViewController
        selectedVC.chartRect = CGRect(x: 0, y: 0, width: ScreenWidth, height: 300)
        selectedVC.view.frame = CGRect(x: 0, y: segmentMenu.frame.maxY, width: ScreenWidth, height: 300)
        
        addChildViewController(selectedVC)
        if (selectedVC.view.superview == nil){
            view.addSubview(selectedVC.view)
        }
        selectedVC.didMove(toParentViewController: self)
        
        currentShowingChartVC = selectedVC
    }
}


